package com.starbaba.messageManager.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.messageManager.model.PullBackMessageModel;
import com.starbaba.model.dictionary.Constant;

@SuppressWarnings({"deprecation", "rawtypes"})
@Component
public class PullBackMessageService {

	private static final SimpleDateFormat FULL_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	
	BaseDao dao = new BaseDao(Constant.POOL_LIFE);

	public String sendMessage(Map<String, Object> paramsMap)  {
		
		String down = (String) paramsMap.get("down");
		String id = (String) paramsMap.get("id");
		String day_mode = (String) paramsMap.get("day_mode");
		String content = (String) paramsMap.get("content");
		String remark = (String) paramsMap.get("remark");
		String mail = (String) paramsMap.get("mail");
		String prd_id = (String) paramsMap.get("prd_id");
		String operator = (String) paramsMap.get("operator");
	
		Date now = new Date();
		String time = FULL_DATE_FORMAT.format(now);
		String date = DATE_FORMAT.format(now);
		String isExists = "select * from message_pull_back_template where status = '1' and day_mode = ? and prd_id = ? ";
		
		if (StringUtils.isNotEmpty(down)){
			try {
				PullBackMessageModel model = dao.get("select * from message_pull_back_template where id = ? ", PullBackMessageModel.class, down);
				model.setStatus(0);
				dao.update(model, "id");
			} catch (Exception e) {
				e.printStackTrace();
				return "回拉短信模版下线失败！";
			}
			return "回拉短信模版下线成功！";
		}
		
		if (StringUtils.isNotEmpty(id)){
			PullBackMessageModel model = new PullBackMessageModel(time, date, 1, Integer.valueOf(day_mode), prd_id, content, operator, remark, mail);
			try {
				model.setId(Long.valueOf(id));
				dao.update(model, "id");
			} catch (Exception e) {
				return "回拉短信模版修改失败！";
			}
			return "回拉短信模版修改成功！";
		}
		
		try {
			PullBackMessageModel oldModel = dao.get(isExists, PullBackMessageModel.class, day_mode, prd_id);
			if (null != oldModel) {
				oldModel.setStatus(0);
				dao.update(oldModel, "id");
			}
			PullBackMessageModel model = new PullBackMessageModel(time, date, 1, Integer.valueOf(day_mode), prd_id, content, operator, remark, mail);
			dao.add(model);
		} catch (Exception e) {
			return "回拉短信模版保存失败！";
		}
		return "回拉短信模版保存成功！";
		
	}

	public void showPage(Map<String, Object> paramsMap) throws AppException {
		List<PullBackMessageModel> typeList = dao.list("select distinct day_mode from message_pull_back_template", PullBackMessageModel.class);
		paramsMap.put("typeList", typeList);
	}

	public List getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws AppException {

		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		String day_mode = (String) paramsMap.get("type");		
		
		StringBuilder whereParams = new StringBuilder();
		if (StringUtils.isNotEmpty(startTime)) {
			whereParams.append("and cdate >= '" + startTime + "' ");
		}
		if (StringUtils.isNotEmpty(endTime)) {
			whereParams.append("and cdate <= '" + endTime + "' ");
		}
		if (StringUtils.isNotEmpty(day_mode)) {
			whereParams.append("and day_mode = '" + day_mode + "' ");
		}
		
		StringBuilder sql_list = new StringBuilder("select * from message_pull_back_template where 1 = 1 ");
		sql_list.append(whereParams).append("order by ctime desc ");
		
		String sqlCount = "select count(1) from (" + sql_list.toString() + ") b ";
		if (start != null && length != null) {
			sql_list.append("limit " + start + ", " + length);
		}
		
		int count = dao.getInt(sqlCount);
		List<PullBackMessageModel> list = dao.list(sql_list.toString(), PullBackMessageModel.class);
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		
		return list;
	}

	public void showSendPullBackMessagePage(Map<String, Object> paramsMap) {
		try {
			String id = (String) paramsMap.get("id");
			if (StringUtils.isNotEmpty(id)) {
				PullBackMessageModel model = dao.get("select * from message_pull_back_template where id = ? ", PullBackMessageModel.class, id);
				paramsMap.put("model", model);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}